const Router = require('koa-router');
const {db, Op,QueryTypes } = require("../../models/db");
const tool = require("../../tools/tool");
const router = new Router();

// 查询商品库存
router.post('/app/searchGoodStock', async ctx => {
    let formData = ctx.request.body.formData;
    let currentPage = ctx.request.body.currentPage?ctx.request.body.currentPage:1;
    let pageSize = 20;
    let offset = (currentPage-1) *pageSize;
    let tenantId = ctx.user.tenantId;
    let searchTxt = tool.xe.trim(formData.searchTxt)
    let listSql = `select * from v_goods where tenantId=${tenantId}`
    if(searchTxt) {
        listSql += ` and(name like '%${searchTxt}%' or code ='${searchTxt}') `;
    }
    listSql += ` order by id desc offset ${offset} rows fetch next ${pageSize} rows only;`
    let list = await db.query(listSql, {type: QueryTypes.SELECT})
    return ctx.suc('', list)

})

module.exports = router;
